<?php 
	include_once '../session.php';
	include_once '../autoload.php';
	
	$lang="en";
	if(isset($_GET['lang'])){$lang=$_GET['lang'];}
	include '../lang/'.$lang.'.lang.php';
	
	$user = $_SESSION['id'];

	$origin_id 	= $_POST['trfOrigin'];
	$destiny_id = $_POST['trfDestiny'];
	$currency	= $_POST['trfCurrencyId'];
	$amount		= $_POST['trfAmount'];
	$type		= $_POST['trfType'];
	$balance	= $_POST['trfBalanceOr'];
	$msg = "";
	
	if($amount > 0 && ($balance - $amount) > -300){
		$client_or = new account();
		$client_de = new account();
		
		$client_or->load($origin_id);
		$client_de->load($destiny_id);
		
		$balance_or = $client_or->getBALANCE();
		$balance_de = $client_de->getBALANCE();
		
		$balanceout_or = $balance_or - $amount;
		$balanceout_de = $balance_de + $amount;
		$client_or->setBALANCE($balanceout_or);
		$client_de->setBALANCE($balanceout_de);
		
		$modified_or = $client_or->modify($origin_id);
		$modified_des = $client_de->modify($destiny_id);
		
		if($modified_or == true && $modified_des == true){
			$msg = $TXT_TRANSFER_COMPLETED;
			$history = new history();
			$history->setACCOUNT_ORIGIN($client_or->getACCOUNT_NUM());
			$history->setACCOUNT_DESTINY($client_de->getACCOUNT_NUM());
			$history->setTRANSACTION_TYPE_ID($type);
			$history->setCURRENCY_ID($currency);
			$history->setAMOUNT($amount);
			$history->insert();
		}else{
			$msg = $TXT_TRANSFER_FAILED;
		}
	}else{
		if($amount <= 0){ $msg= $TXT_TRANSFER_FORBIDEN; }
		if(($balance - $amount) < -300){ $msg= $TXT_TRANSFER_NO_MONEY; }
	}
	echo $msg;
?>